#include <iostream>

/**
 * @brief 测试委托构造函数 Delegating constructor
 * 
 */
class TestDelegatingCon
{
    public:

    //被委托构造函数
    TestDelegatingCon(int x):x_(x){}

    //委托构造函数
    TestDelegatingCon(int x, int y):TestDelegatingCon(x){
        y_ = y;
        std::cout << "TestDelegatingCon(int x, int y):TestDelegatingCon(x)" << std::endl;
    }   

     //委托 委托构造函数
    TestDelegatingCon(int x, int y, int z):TestDelegatingCon(x,y){
        z_ = z;
        std::cout << "TestDelegatingCon(int x, int y, int z):TestDelegatingCon(x,y)" << std::endl;
        }

    // TestDelegatingCon(const TestDelegatingCon& other) : TestDelegatingCon(other.x_){
    //     std::cout << "Copy constructor" << std::endl;
    // }

    TestDelegatingCon(const TestDelegatingCon& other): TestDelegatingCon(other.x_ , other.y_)
    {
        std::cout << "Copy and delegating constructor" << std::endl;
    }       

    // 禁止拷贝构造函数隐式转换
    // TestDelegatingCon(const TestDelegatingCon& other) = delete;

    int x_;
    int y_;
    int z_;
    private:
    int test;
};

// 发现了在构造时，默认给值的行为
// 尝试分析这种行为
class TestDelegatingCon2
{
public:
    TestDelegatingCon2(int x) : x_(x) {}
    TestDelegatingCon2(int x, int y) : x_(x)
    {
        y_ = y;
    }

void printVar() const{
    std::cout << "x_ = " << x_ << std::endl;
    std::cout << "y_ = " << y_ << std::endl;
}

private:
    int x_;
    int y_;

    TestDelegatingCon2(const TestDelegatingCon2& other);
    TestDelegatingCon2& operator=(const TestDelegatingCon2& other);
};


int main()
{
    TestDelegatingCon tdc(10);
    std::cout << "tdc.x_ = " << tdc.x_ << std::endl;

    TestDelegatingCon tdc2(10, 20);
    std::cout << "tdc2.x_ = " << tdc2.x_ << std::endl;
    std::cout << "tdc2.y_ = " << tdc2.y_ << std::endl;

    TestDelegatingCon tdc3(10, 20, 30);
    std::cout << "tdc3.x_ = " << tdc3.x_ << std::endl;
    std::cout << "tdc3.y_ = " << tdc3.y_ << std::endl;
    std::cout << "tdc3.z_ = " << tdc3.z_ << std::endl;

    //拷贝构造
    TestDelegatingCon tdc4(tdc);
    std::cout << "tdc4.x_ = " << tdc4.x_ << std::endl;
    // 这里的y被默认构造了
    std::cout << "tdc4.y_ = " << tdc4.y_ << std::endl;
 
     //拷贝构造
    TestDelegatingCon tdc5(tdc2);
    std::cout << "tdc5.x_ = " << tdc5.x_ << std::endl;
    // 这里的y被默认构造了
    std::cout << "tdc5.y_ = " << tdc5.y_ << std::endl;
    std::cout << "tdc5.z_ = " << tdc5.z_ << std::endl;//z被默认构造了


    // 分析默认给值的行为
    TestDelegatingCon2 tdc12(10);
    tdc12.printVar();
    TestDelegatingCon2 tdc22(20, 30);
    tdc22.printVar();

    // TestDelegatingCon2 tdc32(tdc12); // 编译错误，禁止拷贝构造函数
    // TestDelegatingCon2 tdc42 = tdc22; // 编译错误，禁止拷贝赋值运算符

    return 0;
}